#pragma once
#include "GameComponent.h"
#include "Rigidbody.h"
#include "physics/Physics.h"
#include "SpriteAnimation.h"
#include "Event_PlayerDied.h"




class DonkeyKongController : public Boon::GameComponent
{
	GAMECOMPONENT( DonkeyKongController, Boon::GameComponent )
	
public:
	void Attack()
	{
		m_SpawnBarrel = true;
		m_TimerBarrelSpawn = .4f;
		m_SpriteAnimation->Play( "Attack" );
	}

private:
	virtual void OnCreate() 
	{
		m_Collider = nullptr;
		m_SpriteAnimation = nullptr;
		m_SpawnBarrel = false;
		m_TimerBarrelSpawn = .0f;
	}

	virtual void OnEnable() 
	{
		Boon::EventListener listener( this, &DonkeyKongController::OnPlayerDead );
		Boon::Messenger::GetInstance().Register( Event_PlayerDied::ID, listener );
	}

	virtual void OnDisable() 
	{
		Boon::EventListener listener( this, &DonkeyKongController::OnPlayerDead );
		Boon::Messenger::GetInstance().Remove( Event_PlayerDied::ID, listener );
	}

	virtual void OnStart() 
	{
		m_Collider = GetGameObject().GetCollider();
		m_SpriteAnimation = GetGameObject().GetComponent<SpriteAnimation>().Lock().Get();
		BOON_ASSERT( m_Collider != nullptr )
		BOON_ASSERT( m_SpriteAnimation != nullptr )

	}

	virtual void Update() 
	{
		if( m_SpawnBarrel )
		{
			m_TimerBarrelSpawn -= Boon::Timer::GetInstance().DeltaTime();
			if( m_TimerBarrelSpawn <= 0 )
			{
				m_SpawnBarrel = false;
				SpawnBarrel();
			}
		}
	}

	virtual void LateUpdate() 
	{

	}


	virtual void OnDestroy() 
	{

	}

	void		SpawnBarrel()
	{
		GameObject& barrel = *GameObject::Create( "Le_Barrel" ).Lock().Get();
		barrel.GetTransform().SetPosition( GetGameObject().GetTransform().GetPosition() + Vector3( -5, 3, 0 ) );
		barrel.AddComponent<Rigidbody>().Lock()->SetConstraints( RIGC_FREEZE_POS_Z | RIGC_FREEZE_ROT_X | RIGC_FREEZE_ROT_Y, true );
		barrel.AddComponent<ColliderSphere>().Lock()->SetRadius( 3 );
		barrel.AddComponent<PhysicsRenderer>();
		SpriteAnimation& sa = *barrel.AddComponent<SpriteAnimation>().Lock().Get();

		sa.GetMaterial().SetBounds( Bounds( Vector3::ZERO, Vector3::ONE * 6 ));
		sa.GetMaterial().SetImagePath( "data/sprite/objects.png" );
		sa.CreateClip( "Barrel", Vector2( 4, 1692 ), Vector2( 36, 45 ), 8, 12, WM_LOOP );
		sa.Play( "Barrel" );
		barrel.GetComponent<Rigidbody>().Lock()->AddForce( 30, 50, 0, FM_IMPULSE );
	}

	void OnPlayerDead( const Boon::IMessengerEventStrong& eventData )
	{
		m_SpriteAnimation->Play( "Idle" ); // play to have it set as previous animation
		if( static_cast<Event_PlayerDied*>( eventData.Get() )->m_DeadCount % 2 == 0 )
			m_SpriteAnimation->Play( "FacePalm" );
		else
			m_SpriteAnimation->Play( "IamBest" );
	}

	Collider*					m_Collider;
	SpriteAnimation*			m_SpriteAnimation;
	bool						m_SpawnBarrel;
	float						m_TimerBarrelSpawn;
};

